package com.lrs.java.spring.core.$03_DependencyInject;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ComplexTypeInject {

	private static String classpath = "com/lrs/java/spring/core/$03_DependencyInject/complexTypeInject.xml";
	private static BeanFactory beanFactory;
	static {
		beanFactory = new ClassPathXmlApplicationContext(classpath);
	}
	
	private List<String> listValues;
	private Set<String> setValues;
    private String[] array;
    private String[][] doubleArray;
    private Map<String, String> map;
	private Properties properties;
    
	public List<String> getListValues() {
		return listValues;
	}

	public void setListValues(List<String> listValues) {
		this.listValues = listValues;
	}

	public Set<String> getSetValues() {
		return setValues;
	}

	public void setSetValues(Set<String> setValues) {
		this.setValues = setValues;
	}
	
    public String[] getArray() {
		return array;
	}

	public void setArray(String[] array) {
		this.array = array;
	}

	public String[][] getDoubleArray() {
		return doubleArray;
	}

	public void setDoubleArray(String[][] doubleArray) {
		this.doubleArray = doubleArray;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	//注入java.util.List值
	@Test
	public void testListInject() {
		ComplexTypeInject listBean = beanFactory.getBean("listBean",ComplexTypeInject.class);
		System.out.println(listBean.getListValues().size());
		Assert.assertEquals(3, listBean.getListValues().size());
	}

    //注入java.util.Set值
	@Test
	public void testSetInject() {
		ComplexTypeInject listBean = beanFactory.getBean("setBean",ComplexTypeInject.class);
		System.out.println(listBean.getSetValues().size());
		Assert.assertEquals(4, listBean.getSetValues().size());
	}

    //注入java.util.Collection值
    @Test
    public void testCollectionInject() {
        
        //本质是List类型	Type:java.util.ArrayList
        ComplexTypeInject collectionBean1 = beanFactory.getBean("collectionBean1", ComplexTypeInject.class);
        System.out.println("Type:" + collectionBean1.getListValues().getClass().getName());
        System.out.println(collectionBean1.getListValues().size());
        Assert.assertEquals(3, collectionBean1.getListValues().size());

        //本质是Set类型	Type:java.util.LinkedHashSet
        ComplexTypeInject collectionBean2 = beanFactory.getBean("collectionBean2", ComplexTypeInject.class);
        System.out.println("Type:" + collectionBean2.getSetValues().getClass().getName());
        System.out.println(collectionBean2.getSetValues().size());
        Assert.assertEquals(4, collectionBean2.getSetValues().size());
        
    }

    //注入数组类型值
    @Test
    public void testArrayInject() {
        
    	ComplexTypeInject arrayBean = beanFactory.getBean("arrayBean", ComplexTypeInject.class);
        System.out.println(arrayBean.getArray().length);
        System.out.println(arrayBean.getDoubleArray().length);
        System.out.println(arrayBean.getDoubleArray()[0].length);
        System.out.println(arrayBean.getDoubleArray()[1].length);
        
    }
    
    //注入Map类型值
    @Test
    public void testMapInject() {
    	ComplexTypeInject mapBean = beanFactory.getBean("mapBean", ComplexTypeInject.class);
    	System.out.println(mapBean.getMap().size());
    }

    //注入Properties类型值
    @Test
    public void testPropertiesInject() {
        ComplexTypeInject propertiesBean = beanFactory.getBean("propertiesBean", ComplexTypeInject.class);
        System.out.println(propertiesBean.getProperties().size());
        System.out.println(propertiesBean.getProperties().containsValue("22"));

        ComplexTypeInject propertiesBean2 = beanFactory.getBean("propertiesBean2", ComplexTypeInject.class);
        System.out.println(propertiesBean2.getProperties().size());
        System.out.println(propertiesBean2.getProperties().containsKey("1"));
        System.out.println(propertiesBean2.getProperties().containsKey("2"));
        System.out.println(propertiesBean2.getProperties().containsKey("3"));
        System.out.println(propertiesBean2.getProperties().containsKey("4"));
        System.out.println(propertiesBean2.getProperties().containsKey("5"));
        System.out.println(propertiesBean2.getProperties().containsValue("11"));
    }
    
}
